/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.udt.generator.api.typeprocessers.persistencevaluereader;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import lombok.var;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.ThrowStatement;

public class JsonSerializePersistenceValueReadGenerator extends UdtGetPersistenceValueMethodGenerator {
    public JsonSerializePersistenceValueReadGenerator() {
    }

    //JsonConvert.SerializeObject(data)
    @Override
    protected Block buildMethodBody() {
        var returnStatement = ast.newReturnStatement();
        var methodInvocation = ast.newMethodInvocation();
        methodInvocation.setName(ast.newSimpleName("writeValueAsString"));

        var classCreation = ast.newClassInstanceCreation();
        classCreation.setType(new TypeInfo(ObjectMapper.class).getType(ast));
        addImport(ObjectMapper.class.getTypeName());
        methodInvocation.setExpression(classCreation);
        methodInvocation.arguments().add(ast.newSimpleName("data"));

        returnStatement.setExpression(methodInvocation);
        var tryBlock = ast.newBlock();
        tryBlock.statements().add(returnStatement);

        var tryStatement = ast.newTryStatement();
        tryStatement.setBody(tryBlock);

        var catchStatement = ast.newCatchClause();

        var exceptionExpression = ast.newSingleVariableDeclaration();
        exceptionExpression.setName(ast.newSimpleName("e"));
        exceptionExpression.setType(new TypeInfo(JsonProcessingException.class).getType(ast));
        addImport(JsonProcessingException.class.getTypeName());

        catchStatement.setException(exceptionExpression);
        catchStatement.setBody(getException());
        tryStatement.catchClauses().add(catchStatement);

        var block = ast.newBlock();
        block.statements().add(tryStatement);
        return block;
    }

    private Block getException(){
        addImport(CefException.class.getTypeName());
        ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,"udt数据序列化失败","e",true);
//        var creation = ast.newClassInstanceCreation();
//        creation.setType(ast.newSimpleType(ast.newSimpleName("RuntimeException")));
//
//        creation.arguments().add(ExpressionUtils.getStringLiteral(ast, "udt数据序列化失败"));
//        creation.arguments().add(ast.newSimpleName("e"));
//        throwStatement.setExpression(creation);

        var block = ast.newBlock();
        block.statements().add(throwStatement);
        return block;
    }
}
